package com.pomr.util.quartz;

import com.pomr.model.simstorage.SimFlow;
import com.pomr.model.simstorage.SimStorageDetail;
import com.pomr.service.common.CommonService;
import com.pomr.util.common.SIMConnection;
import com.pomr.util.communication.cx215.CX215Back;
import com.pomr.util.communication.cx215.CX215BackBase;
import com.pomr.util.communication.cx215.CX215Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @作者：Administrator
 * @时间：16:23 2018/8/20 0020
 * @用途：定时任务-查询sim卡流量信息
 **/
@Component("simQuzrtz")
public class SimMessageQuartz {
    private static Logger log = LoggerFactory.getLogger(SimMessageQuartz.class);
    @Autowired
    @Qualifier("commonServiceImpl")
    private CommonService commonService;
    @Scheduled(cron = "0 0 */23 * * ?")
    public void job(){
        log.info("------------------定时任务-查询sim卡流量信息--开始--------------------");
        try {
            List<SimStorageDetail> sims = commonService.findAllSims();
            if(sims!=null && sims.size()>0){
                // 清空sim卡流量信息表
                if(!commonService.clearFlows()){
                    log.info("清空流量信息失败");
                    return;
                }
                CX215Param param = new CX215Param();
                param.setAppid(sims.get(0).getAppid());
                param.setHmType("iccids");
                List<Object> hms = new ArrayList<>();
                for(SimStorageDetail detail:sims){
                    hms.add(detail.getIccid());
                    List<SimFlow> flows = new ArrayList<>();
                    if(hms.size()==50){
                        param.setHm(hms);
                        // 每200条进行统一查询一次 并通过iccid更新到数据库
                        String url = "http://api.cmpyun.com/api/serviceAccept/findCardInfoList";
                        CX215Back back= (CX215Back) CX215Back.fromJson(SIMConnection.mainMessage(param, url));
                        System.out.println("插入数量："+back.getCardList().size());
                        if(back.getCode()==0){
                            saveFlows(back);
                        }else{
                            log.info("查询错误，原因："+back.getCodeName());
                        }
                        hms.clear();
                        param.setHm(null);
                    }
                }
                if(hms.size() > 0){
                    param.setHm(hms);
                    String url = "http://api.cmpyun.com/api/serviceAccept/findCardInfoList";
                    CX215Back back= (CX215Back) CX215Back.fromJson(SIMConnection.mainMessage(param, url));
                    System.out.println("插入数量："+back.getCardList().size());
                    if(back.getCode()==0){
                        saveFlows(back);
                    }else{
                        log.info("查询错误，原因："+back.getCodeName());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info(e.getMessage());
        }
        log.info("------------------定时任务-查询sim卡流量信息--结束--------------------");
    }

    private void saveFlows(CX215Back back) throws Exception{
        List<SimFlow> flows = new ArrayList<>();

        for(CX215BackBase base:back.getCardList()){
            SimFlow flow = new SimFlow();
            flow.setOutdata(base.getOutdata());
            flow.setTotaldata(base.getTotaldata());
            flow.setSurplusdata(base.getSurplusdata());
            flow.setUpdateTime(new Date());
            flow.setIccid(base.getIccid());
            flows.add(flow);
        }
        if(commonService.saveFlows(flows)){
            flows.clear();
        }else{
            log.info("保存流量信息失败");
            flows.clear();
        }
    }
}
